package com.weaver.drools.business.sys.login;

import java.util.List;
import java.util.Map;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.AccountException;
import org.apache.shiro.authc.DisabledAccountException;
import org.apache.shiro.authc.IncorrectCredentialsException;
import org.apache.shiro.authc.UnknownAccountException;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.weaver.drools.business.online.service.UserOnlineService;
import com.weaver.drools.business.sys.permission.service.ISysPermissionService;
import com.weaver.drools.common.entity.SysPermission;
import com.weaver.drools.common.entity.SysUserOnline;
import com.weaver.drools.common.utils.ControllerUtil;
import com.weaver.drools.common.utils.MsgUtil;
import com.weaver.drools.common.utils.ReturnMessage;
import com.weaver.drools.common.utils.SystemUtools;

/**
 * 
 * @application name: 登录controller
 * @author: zhouxinlei
 * @time：2018年6月27日 @version：ver 1.1
 */
@Controller
public class LoginController {

	@Resource
	private ISysPermissionService permissionService;
	@Resource
	private UserOnlineService UserOnlineService;
	
	@RequestMapping(value = "/index")
	public String index(HttpServletResponse response, HttpServletRequest request, Pageable pageable) throws Exception {
		List<SysPermission> permissionList = permissionService.findPermissiontMenuList();
		request.setAttribute("permissionList", permissionList);
		request.setAttribute("menuSize", permissionList.size());
		Map<String, String> system = SystemUtools.SystemInfo(request);
		request.setAttribute("system", system);
		List<SysUserOnline> userOnlineList= UserOnlineService.findSysUserOnlineList();
		request.setAttribute("userOnlineList", userOnlineList);
		return "index";
	}

	@RequestMapping("/login")
	public String login(HttpServletRequest request,HttpServletResponse response, Map<String, Object> map) throws Exception {
		return "login";
	}
	/**
	 * ajax登录请求
	 * @param username
	 * @param password
	 * @return
	 */
	@RequestMapping(value ="/ajaxLogin",produces="application/json;charset=UTF-8")
	@ResponseBody
	public void submitLogin(HttpServletRequest request,HttpServletResponse response) {
		String username =request.getParameter("username");
		String password =request.getParameter("password");
		String rememberMe =request.getParameter("rememberMe");
		String cs ="";
		try {
			UsernamePasswordToken token = new UsernamePasswordToken(username, password,rememberMe);
			SecurityUtils.getSubject().login(token);
			cs = ReturnMessage.messageData(ReturnMessage.SUCCESS, "登录成功！",null);
		} catch (Exception e) {
			String msg = "";
				if (UnknownAccountException.class.equals(e.getClass())|| AccountException.class.equals(e.getClass())) {
					msg = MsgUtil.UnknownAccountException;
				} else if (AccountException.class.equals(e.getClass())) {
					msg = MsgUtil.AccountException;
				} else if (IncorrectCredentialsException.class.equals(e.getClass())) {
					msg = MsgUtil.IncorrectCredentialsException;
				} else if ("kaptchaValidateFailed".equals(e.getClass())) {
					msg = MsgUtil.kaptchaValidateFailed;
				} else if (DisabledAccountException.class.equals(e.getClass())) {
					msg = MsgUtil.SHIRO_IS_LOCK;
				}
			cs = ReturnMessage.messageData(ReturnMessage.FAIL, msg,null);
		}
		ControllerUtil.writejsonAndSession(response,request,cs);
	}
	//踢出用户
	@RequestMapping(value="kickouting")
	@ResponseBody
	public String kickouting() {
		return "error/kickout";
	}
	//被踢出后跳转的页面
	@RequestMapping(value="kickout")
	public String kickout() {
		return "error/kickout";
	}
	@RequestMapping("/unauthorized")
	public String unauthorized() {

		return "error/unauthorized";
	}
}